package com.duowan.realtime.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.duowan.realtime.model.BloomfilterGroup;
import com.duowan.realtime.model.OverviewComputingGroup;
import com.duowan.realtime.model.ServerInfo;
import com.duowan.realtime.service.BloomfilterGroupService;
import com.duowan.realtime.service.OverviewBloomfilterService;
import com.duowan.realtime.service.OverviewComputingGroupService;
import com.duowan.realtime.service.ServerInfoService;


@Controller
public class RealtimeComputingController {
	private static final Logger LOGGER = LoggerFactory.getLogger(RealtimeComputingController.class);
	private ObjectMapper objectMapper = new ObjectMapper();
	
	
	@Autowired
	private BloomfilterGroupService bloomfilterGroupService;
	@Autowired
	private ServerInfoService serverInfoService;
	@Autowired
	private OverviewComputingGroupService overviewComputingGroupService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request, ModelMap model) {
		return "/index";
	}
	
	@RequestMapping("/showBFGroup")
	public String showBFGroup(HttpServletRequest request, ModelMap model) {
		List<BloomfilterGroup> bfGroupList = bloomfilterGroupService.getAll();
		model.put("bfGroupList", bfGroupList);
		return "/showBFGroup";
	}
	
	@RequestMapping
	public void addBFGroup(BloomfilterGroup bloomfilterGroup, HttpServletRequest request,HttpServletResponse response) throws IOException {
		Map<String,String> result = new HashMap<String, String>();
		PrintWriter writer = response.getWriter();
		try {
			bloomfilterGroupService.create(bloomfilterGroup);
			result.put("status", "1");
		}catch(Exception e) {
			LOGGER.error("insert get some wrong ", e);
			result.put("status", "0");
			result.put("message", e.getMessage());			
		}
		writer.write(objectMapper.writeValueAsString(result));
	}
	
	@RequestMapping
	public void deleteBFGroup(int id, HttpServletRequest request,HttpServletResponse response) throws IOException {
		Map<String,String> result = new HashMap<String, String>();
		PrintWriter writer = response.getWriter();
		try {
			bloomfilterGroupService.removeById(id);
			result.put("status", "1");
		}catch(Exception e) {
			LOGGER.error("remove the" + id + " bfgroup get some wrong ", e);
			result.put("status", "0");
			result.put("message", e.getMessage());			
		}
		writer.write(objectMapper.writeValueAsString(result));
	}
	
	@RequestMapping("/showServerInfo")
	public String showServerInfo(HttpServletRequest request, ModelMap model) {
		List<ServerInfo> serverInfoList = serverInfoService.getAll();
		model.put("serverInfoList", serverInfoList);
		return "/showServerInfo";
	}
	
	@RequestMapping
	public void addServerInfo(ServerInfo serverInfo, HttpServletRequest request,HttpServletResponse response) throws IOException {
		Map<String,String> result = new HashMap<String, String>();
		PrintWriter writer = response.getWriter();
		try {
			serverInfoService.create(serverInfo);
			result.put("status", "1");
		}catch(Exception e) {
			LOGGER.error("insert get some wrong ", e);
			result.put("status", "0");
			result.put("message", e.getMessage());			
		}
		writer.write(objectMapper.writeValueAsString(result));
	}
	
	@RequestMapping
	public void deleteServerInfo(int id, HttpServletRequest request,HttpServletResponse response) throws IOException {
		Map<String,String> result = new HashMap<String, String>();
		PrintWriter writer = response.getWriter();
		try {
			serverInfoService.removeById(id);
			result.put("status", "1");
		}catch(Exception e) {
			LOGGER.error("insert get some wrong ", e);
			result.put("status", "0");
			result.put("message", e.getMessage());			
		}
		writer.write(objectMapper.writeValueAsString(result));
	}
	
	@RequestMapping("/showOverviewOCG")
	public String showOverveiewBF(HttpServletRequest request, ModelMap model) {
		List<OverviewComputingGroup> ocgList = overviewComputingGroupService.getAll();
		model.put("ocgList", ocgList);
		return "/showOverviewOCG";
	}
}
